/*
 * =====================================================================================
 *
 *       Filename:  timestamp.h
 *
 *    Description:  based on gettimeofday  
 *    				precision is  microsecond
 *    				used in the whole network lib
 *
 *        Version:  1.0
 *        Created:  2012年08月18日 20时07分20秒
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef TIMESTAMP_H
#define TIMESTAMP_H
#include<stdint.h>//for int64_t
#include<sys/time.h>
#include<string>
namespace inm
{

	namespace net
	{
		//store the microseconds from epoch
		class Timestamp
		{
			public:
				Timestamp(int64_t t=0):
					microSeconds_(t)
				{
				}
				static Timestamp now();//get time span from Epoch
	
				int64_t getMicroSeconds()
				{
					return microSeconds_;
				}
				int64_t getMilliSeconds()
				{
					return microSeconds_ / MilliSecondsPerOneSecond;
				}
				time_t getSeconds()//use time_t for localtime--precision is second 
				{
					return static_cast<time_t>(microSeconds_ / MicroSecondsPerOneSecond);	
				}
				std::string toString();
				inline bool operator < (Timestamp& t)
				{
					return microSeconds_ < t.getMicroSeconds();				
				}
				inline bool operator > (Timestamp& t)
				{
					return microSeconds_ > t.getMicroSeconds();
				}
				inline bool operator == (Timestamp& t)
				{
					return microSeconds_  == t.getMicroSeconds();
				}
			private:
				static const int MilliSecondsPerOneSecond = 1000;
				static const int MicroSecondsPerOneSecond = 1000*1000;
				struct timeval tv_;
				int64_t microSeconds_;
			
		};
	};
};


#endif
